﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Ast.Framework.FileSystems.Paths
{
    public class DefaultPathProvider : IPathProvider
    {
        public string Normalize(string path)
        {
            return Path.GetFullPath(new Uri(path).LocalPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
        }

        public virtual string GetDirectoryName(string path)
        {
            return Path.GetDirectoryName(path);
        }

        public virtual IEnumerable<string> ListFiles(string path)
        {
            return Directory.GetFiles(path);
        }

        public virtual IEnumerable<string> ListDirectories(string path)
        {
            return Directory.GetDirectories(path);
        }

        public virtual string Combine(params string[] paths)
        {
            var path = Path.Combine(paths);
            return Path.IsPathRooted(path) ? Normalize(path) : path;
        }

        public virtual Stream OpenFile(string path)
        {
            return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        }

        public virtual StreamWriter CreateText(string path)
        {
            return File.CreateText(path);
        }

        public virtual Stream CreateFile(string path)
        {
            return File.Create(path);
        }

        public virtual DateTime GetFileLastWriteTimeUtc(string path)
        {
            return File.GetLastWriteTimeUtc(path);
        }

        public virtual void DeleteFile(string path)
        {
            File.Delete(path);
        }

        public virtual bool FileExists(string path)
        {
            return File.Exists(path);
        }

        public virtual bool DirectoryExists(string path)
        {
            return Directory.Exists(path);
        }

        public virtual void CreateDirectory(string path)
        {
            Directory.CreateDirectory(path);
        }

        public virtual void DeleteDirectory(string path)
        {
            Directory.Delete(path);
        }
    }
}